<#include "../common/header.htm">
<div class="content-wrapper">
	<section class="content-header">
		<ol class="breadcrumb">
			<li><a href="${ctx}/">首页</a></li>
			<li class="active"><a href="#">卡券管理</a></li>
		</ol>
	</section>
	<section class="content">
		<div class="box">
			<form action="${ctx}/cardtype/managelist" method="get" id="listForm">
				<div class="box-header">
					<div class="row RowSearchWork">
						<div class="col-sm-12">
						    <div class="col-sm-2">
								<label>使用用途：</label> 
								<select class="form-control" name="cardUse">
								   <option value="">请选择</option>
								   <#list cardUseMap as key, value>
										<#if cardUse?? && key == cardUse?c>
											<option value="${key}" selected="selected">${value}</option>
										<#else>
											<option value="${key}">${value}</option>
										</#if> 
								   </#list>
								 </select>
							</div>
						    <div class="col-sm-2">
								<label>折扣类型：</label> 
								<select class="form-control" name="preferentialType">
									<option value="">请选择</option>
									<#list preferentialTypeMap as key, value>
										<#if preferentialType?? && key == preferentialType?c>
											<option value="${key}" selected="selected">${value}</option>
										<#else>
											<option value="${key}">${value}</option>
										</#if> 
									</#list>
								</select>
							</div>
						    <div class="col-sm-2">
								<label>状态：</label> 
								<select class="form-control" name="cardStatus">
									<option value="">请选择</option>
									<#list cardStatusMap as key, value>
										<#if cardStatus?? && key == cardStatus?c>
											<option value="${key}" selected="selected">${value}</option>
										<#else>
											<option value="${key}">${value}</option>
										</#if> 
									</#list>
								</select>
							</div>
							<div class="col-sm-3">
								<input type="text" class="form-control" placeholder="请输入使用用途/门槛/卡券名称/卡券说明" name="keyword" value="${keyword!}"/>
							</div>
							<div class="col-sm-1">
								<input type="button"  class="btn btn-default btn-search" onclick="cardtypes.query()" value="查询" />
							</div>
							<div class="col-sm-1">
								<input type="button"  class="btn btn-primary" onclick="cardtypes.form()" value="新增" />
							</div>
						</div>
					</div>
				</div>
				<div class="box-body">
					<table class="table table-hover table-bordered table-striped table-manage">
						<thead>
							<tr>
								<th style="width:80px;">序号</th>
								<th style="width:10%;">编号</th>
								<th style="width:10%;">卡券名称</th>
								<th style="width:10%;">使用用途</th>
								<th style="width:10%;">折扣类型</th>
								<th style="width:10%;">打折/抵扣金额</th>
								<th style="width:10%;">使用期限(天)</th>
								<th style="width:10%;">卡券说明</th>
								<th style="width:10%;">创建时间</th>
								<th style="width:10%;">状态</th>
								<th style="width:160px;">操作</th>
							</tr>
						</thead>
						<tbody>
							<#list page.content as p>
							<tr>
								<td>${page.size * page.number + p_index+1}</td>
								<td>${p.cardNo!}</td>
								<td>${p.cardName!}</td>
								<td style="overflow: inherit; text-overflow: inherit; white-space: inherit;">
									<#if p.cardUseNameList??>
										<#list p.cardUseNameList as cun>
											${cun}<#if cun_index lt (p.cardUseNameList?size - 1)>/</#if>
										</#list>
									<#else>
										--
									</#if>
								</td>
								<td><@mymacro.mapGet preferentialTypeMap p.preferentialType></@mymacro.mapGet></td>
								<td>${p.preferentialIntensity!}</td>
								<td>
									<#if p.useDuration?? && p.useDuration == 0>
										无期限
									<#else>
										${p.useDuration!}
									</#if>
								</td>
								<td title="${p.cardDesc!}">${p.cardDesc!}</td>
								<td><@mymacro.dateFormat p.createTime "yyyy-MM-dd HH:mm:ss"></@mymacro.dateFormat></td>
								<td><@mymacro.mapGet cardStatusMap p.cardStatus></@mymacro.mapGet></td>
								<td>
									<#if p.cardStatus == 0>
										<a href="javascript:void(0)" onclick="cardtypes.modifyStatus('${p.cardId}', 1)">启用</a>
									<#else>
										<a href="javascript:void(0)" onclick="cardtypes.grant('${p.cardId}')">发放</a>&nbsp;&nbsp;
										<a href="javascript:void(0)" onclick="cardtypes.modifyStatus('${p.cardId}', 0)">停用</a>
									</#if>
								</td>
							</tr>
							</#list>
						</tbody>
					</table>
					<#include "../common/page.htm">
				</div>
			</form>
		</div>
	</section>
</div>

<script type="text/javascript">
	$('.select2').select2();
	$.fn.modal.Constructor.prototype.enforceFocus = function() {};
	
	var is_submit=false;
	var cardtypes = {
		query : function() {
			$(":input[name='pageNumber']").val(0);
			$("form").submit();
		},
		modifyStatus : function (cardId, cardStatus) {
			if(is_submit){
				return;
			}
			is_submit=true;
			console.log('cardId='+cardId);
			
			var statusStr = cardStatus == 1 ? '启用' : '停用';
			
			bootbox.confirm("确定要"+statusStr+"吗？", function (result) {
				if (result) {
					var verificationPassed = false;
					$.ajax({
						url : "${ctx}/cardtype/modifyStatus",
						type : 'post',
						dataType : 'json',
						async : false,
						data : {"cardId" : cardId, "cardStatus" : cardStatus},
						success : function(data) {
							if (data.result == 'success') {
								verificationPassed = true;
								$("#listForm").submit();
							}
						}
					});
				}
				is_submit=false;
			});
		},
		grant : function (cardId) {
			if(is_submit){
				return;
			}
			is_submit=true;
			
			cardId = cardId ? cardId : "";
			var title = "发放";
			
			var j = 0;
			base.getView({
				url:"${ctx}/cardtype/toGrantCardCompany",
				success:function(result){
					// console.log(result);
					bootbox.dialog({
						title:title,
						message:result,
						size:"large",
						closeButton : false,
						buttons:{
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
								callback:function(){
									is_submit=false;
								}
							},
							sure:{
								label:"确定",
								className:"btn btn-primary",
								callback:function() {
									if (j != 0) {
										is_submit=false;
										return ;
									}
									j++;
									var params = grantParams();
									params["cardId"] = cardId;
									console.log(params);
									var grantResult = false;
									$.ajax({
										url : "${ctx}/cardtype/doGrantCardCompany",
										type : 'post',
										dataType : 'json',
										async : false,
										data : params,
										success : function(response) {
											if (response.result == 'success') {
												grantResult = true;
											}
										}
									});
									if (grantResult) {
										$("#listForm").submit();
									} else {
										j = 0;
									}
									return false;
								}
							}
						}
					})
				}
			});
		},
		form : function (cardId) {
			if(is_submit){
				return;
			}
			is_submit=true;
			
			cardId = cardId ? cardId : "";
			var title = cardId ? "编辑" : "新增";
			
			var j = 0;
			base.getView({
				url:"${ctx}/cardtype/manageform?cardId="+cardId,
				success:function(result){
					bootbox.dialog({
						title:title,
						message:result,
						size:"large",
						closeButton : false,
						buttons:{
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
								callback:function(){
									is_submit=false;
								}
							},
							sure:{
								label:"确定",
								className:"btn btn-primary",
								callback:function() {
									if (j != 0) {
										is_submit=false;
										return ;
									}
									j++;
									$("#addCardtype").valid();
									var cardUseCount = cardUseCheckedCount();
									if (cardUseCount == 0) {
										cardUseShowError();
										j = 0;
										return false;
									} else {
										cardUseHideError();
									}
									base.validAndAjaxSubmitJson($("#addCardtype"), function () {
										j = 0;
									}, function (data) {
										if (data.result == 'success') {
											$("#listForm").submit();
										} else {
											j = 0;
										}
									});
									return false;
								}
							}
						}
					})
				}
			});
		}
	}
</script>
<#include "../common/footer.htm">